package game.common
{


	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import game.managers.ResourceManager;

	/**
	 * 自定义图片类
	 * @author xiezhicheng
	 */
	public class CustomBitmap extends Sprite
	{
		private var url:String;
		private var noPic:MovieClip;
		private var bmd:BitmapData;
		private var bm:Bitmap;
		private var _cFunc:Function;
		
		private var _source:String;
		
		private static var resourceManager:ResourceManager=ResourceManager.getInstance();

		public function CustomBitmap()
		{
			this.mouseEnabled=true;
			this.mouseChildren=false;
			bm=new Bitmap();
			addChild(bm);
		}

		/**
		 * @param value 图片资源完整路径或者图片链接类
		 */
		public function set source(value:String):void
		{
			if (!value || value == "")
				return;
			_source = value;
			if (!resourceManager.has(value))
			{
				var tempExt:String=value.substr(-4, 4);
				if (tempExt == ".png" || tempExt == ".jpg")
				{
					loadPic(value);
				}
				else
				{

					bmd=Reflection.createBitmapDataInstance(value);
					//	bmd=Reflection.createBitmapDataInstance(value);
					//	var mo:MovieClip = Reflection.createBitmapDataInstance(value,_swfLoader)
					resourceManager.add(value, bmd, false);
					//	addChild(mo);
					setBitmapData();
				}
			}
			else
			{
				bmd=resourceManager.getResource(value) as BitmapData;
				resourceManager.add(value, bmd, false);
				setBitmapData();
			}


		}

		/**
		 * 加载图片
		 * @param vurl 图片资源完整路径
		 */
		private function loadPic(vurl:String):void
		{
			if (url == "" && url == "")
				return;
			if (vurl)
			{
				url=vurl;
			}
			if (resourceManager.has(url))
			{
				bmd=resourceManager.getResource(url) as BitmapData;
				setBitmapData();
			}
			else
			{
//				_loader=new ImageLoader(url, {onComplete: setBitmapData});
//				_loader.load();
			}
		}


		private function setBitmapData():void
		{
			if (!bm)
				return;
			bm.bitmapData=bmd;
			if (_cFunc != null)
				_cFunc();
		}


		/**
		 * 设置完成回调函数
		 */
		public function set cFunc(value:Function):void
		{
			_cFunc=value;
		}

		/**
		 * 释放位图内存
		 * */
		public function dispose():void
		{
			url=null;
			if (bmd)
			{
				bmd.dispose();
				bmd=null
			}
			if (bm && bm.bitmapData)
			{
				bm.bitmapData.dispose();
				bm.bitmapData=null;
			}
		}

		public function get source():String
		{
			return _source;
		}

	}
}